$(function(){
    var _mode = "";
    var _membre = "";
    var _enviados = 0;
    var _total = 0;
    var _affilies = Array();
    var _pointer = 0;
    var _message = null;

    /* JQUERY_EXTEND */
    $.extend({ 		
		
        // MEBRES
        initMembre : function(){
            $("#pb1").progressBar({
                width:400
            });
            $("#jqGrid").jqGrid({
                // <MODIFICAR>
                url: './Queries/query-suscriptor.php?nd='+new Date().getTime(),
                colNames:['int_id_suscriptor','var_code','Noms & Pr&eacute;noms', 'Email'],
                colModel:[
                {
                    hidden:true, 
                    search:false, 
                    name:'int_id_suscriptor', 
                    index:'int_id_suscriptor', 
                    align:'left'
                },

                {
                    hidden:true, 
                    search:false, 
                    name:'var_code', 
                    index:'var_code', 
                    align:'left'
                },

                {
                    name:'var_fullname', 
                    index:'var_fullname', 
                    align:'left'
                },

                {
                    name:'var_email', 
                    index:'var_email', 
                    align:'left'
                }
                ],
                sortname: ['int_id_suscriptor'],
                sortorder: "asc",
                width: 746,
                height: 400,
                // </MODIFICAR>
				
                loadtext: 'Chargement..',
                datatype: "json",
                pager: $('#jqpager'),
                rownumbers: true,
                rowNum:950,
                rowList:[950],	
                viewrecords: true,
                multiselect: true
            });
            
        }, 
        deleteContact: function(_ids){
            $.post("./?ws=Newsletter&act=deleteContact", {
                _ids:_ids, 
                cache:new Date().getTime()
            }, function(data){
                if($.trim(data)!=""){
                    $('#jqGrid').trigger('reloadGrid');
                    alert(data);
                }
            });
        },
        // END MEBRES
        getAllAffilies : function(_idNewsletter){
            _enviados = 0;
            _total = 0;
            _pointer = 0;
			
            _total = _affilies.length;
            $("#total").html(_total);
            $("#total2").html(_total);
	
            $.getJSON("./?ws=Newsletter&act=getMessageNewsletter", {
                _idNewsletter:_idNewsletter, 
                cache:new Date().getTime()
            }, function(data){
                if(data!=null){
                    //                    _message = 	data;
                    $.getEnviarNewsLetter(_affilies[_pointer].var_code, _affilies[_pointer].var_fullname, _affilies[_pointer].var_email);
                }
				
            });	
        },
        getProcent: function(init, total){
            return Math.floor(100 * parseInt(init) / parseInt(total));
        },
        getEnviarNewsLetter : function(_code, _nom, _email){
            $.post("./?ws=Newsletter&act=sendMail", {
                _code:_code, 
                _nom:_nom, 
                _email:_email,
                cache:new Date().getTime()
            },
            function(data){
                if(data!=null || data!=""){
                    _enviados++;
                    _pointer++;

                    $("#enviados").html(_enviados);
                    $('#pb1').progressBar( $.getProcent(_enviados, _total) );
					
                    if(_enviados<_total) $.getEnviarNewsLetter(_affilies[_pointer].var_code,_affilies[_pointer].var_fullname, _affilies[_pointer].var_email);
                    else{
                        //$('#jl-view').html( _message );
                        $("#buttonss").show();
                        return false;
                    }
                }
            });
        }

    });
    /* END JQUERY_EXTEND */
	
    $("#id-send-newsletter").click(function(){
        var gr = $("#jqGrid").getGridParam('selrow');									
        if(gr!=null){
            if($('#cbonesletter').val()!=''){
                $("#pb1").progressBar({
                    //                    barImage: 'Resouces/images/progressbg_black.gif',
                    width:400
                });
                var s = $("#jqGrid").getGridParam('selarrrow');
                var f = s.toString();
                var j =  f.split(',');
                _affilies = Array();
				
                $.each(j,function(i,item){
                    var _k = $("#jqGrid").getRowData(item);
                    _affilies.push( _k );
                });

                $("#buttonss").hide();
                $.getAllAffilies( $('#cbonesletter').val() );
                $("#JLModal-newsletter").JLModal({
                    opacity:25,
                    duration:150,
                    titleHTML: "Envoyer la newsletter",
                    escKey:false,
                    closeButton:false
                });
            }
            else{
                $('#cbonesletter').focus();
                alert('S\u00E9lectioneZ la newsletter');
            }
        }
        else
            alert('S\u00E9lectioneZ les affili\u00E9s');
    });
	
    $("#idhide").click(function(){			
        $("#JLModal-newsletter").JLModal.close();
    });

    function selectCbo(_idcbo, _intval){
        $(_idcbo+" option").each(function (){
            if($(this).val() == _intval){
                $(this).attr('selected','selected'); 
            }
        });
    }

    $("#sdelete").click(function(){
        _mode = 'delete';
        var gr = $("#jqGrid").getGridParam('selrow');
		
        if( gr != null ){
            var s = $("#jqGrid").getGridParam('selarrrow');
            var f = s.toString();
            var j =  f.split(',');

            if(confirm("Eliminer affili\u00E9(s)\nVous avez selection\u00E9 "+j.length+" affili\u00E9(s).\nVoulez vous vraiment l'effacer(s)?")){
                $.deleteContact(f);
            }
        }
        else{
            alert('Avertissement\nS\u00E9lectionner les affili\u00E9 \u00E0 effacer.');
        }
    });

    //Initializing
    $.initMembre();
	
});